<template>
  <div class="allotBill">
    <div class="publicSearch">
      <div class="searchBar" :style="{ height: isMoreSearch ? 'auto' : '' }">
        <el-button-group>
          <el-button @click="getSoData" size="mini" icon="el-icon-search"
            >查询</el-button
          >
          <el-button @click="clearSearchData" size="mini" icon="el-icon-refresh"
            >重置</el-button
          >
        </el-button-group>
        <div>
          <span>单号</span>
          <el-input
            v-model="searchData.billNo"
            size="mini"
            placeholder="请输入单号"
            clearable
          ></el-input>
        </div>
        <div>
          <span>调入仓库</span>
          <el-select
              v-model="searchData.toErpAreaId"
              @change="getSoData"
              filterable
              placeholder="请选择调入仓库"
              clearable
              size="small"

          >
            <el-option
                v-for="item in wares"
                :key="item.erpAreaId"
                :label="item.areaName"
                :value="item.erpAreaId"
            >
            </el-option>
          </el-select>
        </div>

<!--          <el-select-->
<!--            v-model="searchData.toWareId"-->
<!--            size="mini"-->
<!--            filterable-->
<!--            placeholder="请选择调入仓库"-->
<!--            clearable-->
<!--          >-->
<!--            <el-option-->
<!--              v-for="item in wares"-->
<!--              :key="item.wareId"-->
<!--              :label="item.wareName"-->
<!--              :value="item.wareId"-->
<!--            >-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        <div>-->
<!--          <span>承运方</span>-->
<!--          <el-select-->
<!--            v-model="searchData.carrierId"-->
<!--            size="mini"-->
<!--            filterable-->
<!--            placeholder="请选择承运方"-->
<!--            clearable-->
<!--          >-->
<!--            <el-option-->
<!--              v-for="item in carriers"-->
<!--              :key="item.carrierId"-->
<!--              :label="item.carrierName"-->
<!--              :value="item.carrierId"-->
<!--            >-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </div>-->

<!--        <div>-->
<!--          <span>审核状态</span>-->
<!--          <el-select-->
<!--            v-model="searchData.caseState"-->
<!--            size="mini"-->
<!--            filterable-->
<!--            placeholder="请选择审核状态"-->
<!--            clearable-->
<!--          >-->
<!--            <el-option-->
<!--              v-for="item in reviewStates"-->
<!--              :key="item.value"-->
<!--              :label="item.label"-->
<!--              :value="item.value"-->
<!--            >-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </div>-->
<!--        <div>-->
<!--          <span>货主</span>-->
<!--          <el-select-->
<!--            v-model="searchData.organizationId"-->
<!--            size="mini"-->
<!--            filterable-->
<!--            placeholder="请选择货主"-->
<!--            clearable-->
<!--          >-->
<!--            <el-option-->
<!--              v-for="item in organizations"-->
<!--              :key="item.organizationId"-->
<!--              :label="item.organizationName"-->
<!--              :value="item.organizationId"-->
<!--            >-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </div>-->
<!--        <div>-->
<!--          <span>客户</span>-->
<!--          <el-select-->
<!--            v-model="searchData.customerId"-->
<!--            size="mini"-->
<!--            filterable-->
<!--            placeholder="请选择客户"-->
<!--            style="width: 70%"-->
<!--          >-->
<!--            <el-option-->
<!--              v-for="item in customers"-->
<!--              :key="item.customerId"-->
<!--              :label="item.customerName"-->
<!--              :value="item.customerId"-->
<!--            >-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </div>-->
        <div>
          <span>创建时间</span>
          <el-date-picker
            v-model="searchData.createTimeArr"
            size="mini"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
<!--        <div>-->
<!--          <span>要求交货时间</span>-->
<!--          <el-date-picker-->
<!--            v-model="searchData.deliveryTimeArr"-->
<!--            size="mini"-->
<!--            type="daterange"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            range-separator="至"-->
<!--            start-placeholder="开始日期"-->
<!--            end-placeholder="结束日期"-->
<!--          >-->
<!--          </el-date-picker>-->
<!--        </div>-->
<!--        <div>-->
<!--          <span>预期发货时间</span>-->
<!--          <el-date-picker-->
<!--            v-model="searchData.expectTimeFmArr"-->
<!--            size="mini"-->
<!--            type="daterange"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            range-separator="至"-->
<!--            start-placeholder="开始日期"-->
<!--            end-placeholder="结束日期"-->
<!--          >-->
<!--          </el-date-picker>-->
<!--        </div>-->
<!--        <div>-->
<!--&lt;!&ndash;          <span>优先级</span>&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input&ndash;&gt;-->
<!--&lt;!&ndash;            v-model="searchData.orderNo"&ndash;&gt;-->
<!--&lt;!&ndash;            size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;            placeholder="请输入优先级"&ndash;&gt;-->
<!--&lt;!&ndash;            clearable&ndash;&gt;-->
<!--&lt;!&ndash;          ></el-input>&ndash;&gt;-->
<!--        </div>-->
<!--        <div>-->
<!--          <span>关键字搜索</span>-->
<!--          <el-input-->
<!--            v-model="searchData.keyWords"-->
<!--            size="mini"-->
<!--            title="自定义字段/创建人/备注"-->
<!--            placeholder="自定义字段/创建人/备注"-->
<!--            clearable-->
<!--          ></el-input>-->
<!--        </div>-->
      </div>
<!--      <span-->
<!--        class="moreSearchBtn"-->
<!--        :class="isMoreSearch ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"-->
<!--        @click="isMoreSearch = !isMoreSearch"-->
<!--      ></span>-->
    </div>

    <div class="headerBtns">
      <el-button
        size="mini"
        type="primary"
        round
        icon="iconfont icon-xinzeng"
        @click="$router.push({ name: 'createAllotBill' })"
      >
        新增</el-button
      >

    </div>

    <div class="tableBox">
      <el-table
          border
        highlight-current-row
        :data="allotBills"
        @selection-change="handleSelectionChange"
        style="width: 100%;padding-left: 0"
      >
        <el-table-column type="selection" align="center">
        </el-table-column>
        <el-table-column
            label="序号"
            fixed="left" type="index" align="center"
            width="80"
        >
        </el-table-column>
        <el-table-column
          fixed="left"
          sortable
          show-overflow-tooltip
          prop="billNo"
          label="单号"
          align="center"
        >
          <template slot-scope="scope">
            <span
              class="textBtn"
              @click="
                $router.push({ name: 'allotBillDetail', params: scope.row })
              "
              >{{ scope.row.billNo }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="fromAreaName"
          label="调出仓库"
          align="center"
        >

        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="toAreaName"
          label="调入仓库"
          align="center"
        >
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="asnState" label="入库状态" width="100">
          <template slot-scope="scope">
            <el-tag
                v-for="(item, index) in asnStates"
                :key="index"
                v-if="scope.row.asnState === item.value"
                :type="item.type"
            >{{ item.label }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="soState" label="出库状态" width="100">
          <template slot-scope="scope">
            <el-tag
                v-for="(item, index) in soStates"
                :key="index"
                v-if="scope.row.soState === item.value"
                :type="item.type"
            >{{ item.label }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="createTime"
          label="创建时间"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.createTime.substr(5, 11) }}
          </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="createUserName"
          label="创建人"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.createUserId,
                  'account'
                )
              "
              type="text"
              >{{ scope.row.createUserName }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="memo"
          label="备注"
          align="center"
        >
        </el-table-column>
        <el-table-column
            fixed="right"
            sortable
            show-overflow-tooltip
            prop=" "
            label="操作"

        >
          <template slot-scope="scope">
            <el-dropdown
                style="margin-left: 10px; margin-right: 10px"
                size="medium"
                type="primary"
                icon="iconfont icon-print"
                @click="demoPrint(scope.row.billNo)"
                split-button
            >
              打印
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                ><el-button
                    size="small"
                    type="primary"
                    @click="$EventBus.$emit('setPrinter')"
                >打印设置</el-button
                ></el-dropdown-item
                >
                <el-dropdown-item
                ><el-button
                    style="margin-top: 2px"
                    size="small"
                    type="primary"
                    @click="demoPrint(scope.row.billNo, 1)"
                >打印预览1</el-button
                ></el-dropdown-item
                >

                <el-dropdown-item
                ><el-button
                    style="margin-top: 2px"
                    size="small"
                    type="primary"
                    @click="demoPrint(scope.row.billNo, 2)"
                >打印预览2</el-button
                ></el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>

      </el-table>
      <el-pagination
        style="text-align: right"
        class="pagination-bottom"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :page-sizes="[15, 30, 50, 100]"
        :current-page.sync="searchData.pageNum"
        :page-size="searchData.pageSize"
        layout="sizes, total, prev, pager, next, jumper"
        :total="searchData.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "AllotBill",
  data() {
    return {
      unLoadData: {
        model: "",
        wareId: 111,
        insertType: 111,
      },

      isMoreSearch: false,
      loading: {
        masterTable: true,
      },
      searchData: {
        toErpAreaId: '',
        pageNum: 1,
        pageSize: 15,
        total: null,
        keyWords: "",
        insertType: 0,
        fromWareId: PF.getLocal("wareId", "number"),
        state: null,
        caseState: null,
        organizationId: null,
        customerId: null,
        toWareId: null,
        billSource: null,
        carrierId: null,
        createTimeArr: [],
        startCreateTime: "",
        endCreateTime: "",
        deliveryTimeArr: [],
        startDeliveryTime: "",
        endDeliveryTime: "",
        expectTimeFmArr: [],
        startExpectTimeFm: "",
        endExpectTimeTo: "",
        billNo: "",
        orderNo: "",
      },

      allotBills: [],

      // formLabelWidth: "120px",

      allotUdfHs: [],
      organizations: [],
      customers: [],
      carriers: [],
      multipleSelection: [],
      wares: [],
    };
  },
  computed: {

    ...mapState(["reviewStates", "asnStates", "qcStates","soStates"]),
    mergeFlg() {
      let list = PF.JSON(this.multipleSelection);
      let flg = false;
      if (list.length > 1) {
        for (let i = 0; i < list.length; i++) {
          if (
            list[i].wareId !== list[0].wareId ||
            list[i].organizationId !== list[0].organizationId ||
            list[i].customerId !== list[0].customerId ||
            list[i].carrierId !== list[0].carrierId ||
            list[i].receivingAddressId !== list[0].receivingAddressId ||
            list[i].billSource === 2
          ) {
            flg = true;
          }
        }
        return flg;
      } else {
        return true;
      }
    },
  },
  methods: {
    downloadModel() {
      window.open("/model/SoImport.xls");
    },

    upLoad() {
      var formData = new FormData();
      formData.append("model", document.querySelector("#file").files[0]); //上传一个files对象
      formData.append("wareId", 111); //需要上传的多个参数

      IOT.upLoadFile(
        "/so/masters/importSoBills",
        "post",
        formData,
        (ret) => {},
        true
      );
    },

    demoPrint: function (billNo, isReview) {
      var that = this;
      IOT.getServerData(
          "/allot/masters/allotPrint/"+billNo,
          "get",
          null,
          (ret) => {
            if (ret.code === 200) {
              var strHTML = ret.data;

              that.$EventBus.$emit("printHtml", strHTML, isReview);
            } else {
              IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
            }
          },
          true
      );
    },
    printTest() {
      var urlStr =
          "http://lazerp2.oss-cn-hangzhou.aliyuncs.com/label/ztoth/77130044426681.pdf";
      this.$EventBus.$emit("printUrl", urlStr);
    },
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.getSoData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getSoData();
    },
    clearSearchData() {
      this.searchData.keyWords = "";
      this.searchData.state = null;
      this.searchData.caseState = null;
      this.searchData.organizationId = null;
      this.searchData.customerId = null;
      this.searchData.billSource = null;
      this.searchData.carrierId = null;
      this.searchData.createTimeArr = [];
      this.searchData.startCreateTime = "";
      this.searchData.endCreateTime = "";
      this.searchData.deliveryTimeArr = [];
      this.searchData.startDeliveryTime = "";
      this.searchData.endDeliveryTime = "";
      this.searchData.expectTimeFmArr = [];
      this.searchData.startExpectTimeFm = "";
      this.searchData.endExpectTimeTo = "";
      this.searchData.billNo = "";
      this.searchData.orderNo = "";
    },
    getSoData() {
      let that = this;
      that.loading.masterTable = true;
      let searchData = PF.JSON(this.searchData);
      if (searchData.createTimeArr.length > 0) {
        searchData.startCreateTime = searchData.createTimeArr[0];
        searchData.endCreateTime = searchData.createTimeArr[1];
      }
      if (searchData.deliveryTimeArr.length > 0) {
        searchData.startDeliveryTime = searchData.deliveryTimeArr[0];
        searchData.endDeliveryTime = searchData.deliveryTimeArr[1];
      }
      if (searchData.expectTimeFmArr.length > 0) {
        searchData.startExpectTimeFm = searchData.expectTimeFmArr[0];
        searchData.endExpectTimeTo = searchData.expectTimeFmArr[1];
      }

      IOT.getServerData("/allot/masters/list", "get", searchData, (ret) => {
        that.loading.masterTable = false;
        if (ret.code === 200) {
          console.log(ret,'accept/records/pdaBatchAcceptaccept/records/pdaBatchAcceptaccept/records/pdaBatchAcceptaccept/records/pdaBatchAccept')
          this.allotBills = ret.rows;
          this.searchData.total = ret.total;
          this.searchData.pageNum = ret.pageNumber;
        } else {
          IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    commitFile() {
      IOT.showOverlay("保存中，请稍等...");
      let list = PF.JSON(this.multipleSelection);
      let billNos = [];
      for (let i = 0; i < list.length; i++) {
        billNos.push(list[i].billNo);
      }
      IOT.getServerData(
        "/so/masters/file",
        "post",
        { billNos: billNos },
        (ret) => {
          IOT.hideOverlay();
          this.multipleSelection = [];
          if (ret.code === 200) {
            IOT.tips("归档成功！", "success");
            this.getSoData();
          } else {
            IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
          }
        }
      );
    },
    commitMerge() {
      IOT.showOverlay("保存中，请稍等...");
      let list = PF.JSON(this.multipleSelection);
      let billNos = [];
    },
  },
  created() {
    this.getSoData();
    PF.getCarriers((rows) => {
      this.carriers = rows;
    });
    PF.areaInfo((rows) => {

      this.wares = rows;
    });
    PF.getSysParamUdfData("allotUdfHs", [], (data, udfs) => {
      this.allotUdfHs = udfs;
    });
    PF.getOrganizations((rows) => {
      this.organizations = rows;
    });
    PF.getCustomers((rows) => {
      this.customers = rows;
    });
  },
  mounted() {},
};
</script>

<style scoped lang="less">
.allotBill {
}
</style>
